package com.baizhiedu.yaml;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;

import java.util.Properties;

/**
 * ClassName: YamlAutoConfiguration
 * Description:
 * Date: 2022/12/04 PM 7:37:32
 * <p>
 * project: spring5
 * package: com.baizhiedu.yaml
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@Configuration
@ComponentScan(basePackages = "com.baizhiedu.yaml")
public class YamlAutoConfiguration {

    @Bean
    public PropertySourcesPlaceholderConfigurer configurer() {
        YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
        yamlPropertiesFactoryBean.setResources(new ClassPathResource("init.yml"));
        Properties properties = yamlPropertiesFactoryBean.getObject();

        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        if (properties != null) {
            configurer.setProperties(properties);
        }
        return configurer;
    }
}
